package net.asfun.jvalog.frame;

import java.util.logging.Level;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.asfun.jvalog.mvc.HandlerInterceptor;
import net.asfun.jvalog.mvc.ModelAndView;


import static net.asfun.jvalog.common.log.logger;

public class ConsoleInterceptor implements HandlerInterceptor {
	

	@Override
	public void afterCompletion(HttpServletRequest req,
			HttpServletResponse resp, Object controller, Exception ex)
			throws Exception {
		if ( ex != null ) {
			logger.log(Level.SEVERE, "error", ex.getCause());
			logger.severe("Request " + req.getRequestURI() +" causes exception：" + ex.getMessage());
			JdoFactory.txRollback();
		}
		try {
			JdoFactory.txCommit();
		} catch (Exception e) {
			logger.severe("Error occur while handlering transaction: " + e.getMessage());
			JdoFactory.txRollback();
		}
		JdoFactory.close();
	}

	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse resp,
			Object controller, ModelAndView mav) throws Exception {
		
	}

	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,
			Object controller) throws Exception {
		resp.setContentType("text/html; charset=utf-8");
//		JdoFactory.txBegin();
		return true;
	}

}
